package com.giscloud.limp.service.impl;

import org.springframework.transaction.annotation.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.giscloud.datasource.page.PageInfo;
import com.giscloud.commons.model.Result;
import cn.hutool.core.collection.CollectionUtil;

import java.util.Date;
import java.util.List;
import com.giscloud.limp.entity.DefendantExample;
import com.giscloud.limp.entity.dto.DefendantDTO;
import com.giscloud.limp.dao.DefendantDao;
import com.giscloud.limp.entity.Defendant;
import com.giscloud.limp.service.DefendantService;
import lombok.extern.log4j.Log4j2;


@Service("defendantService")
@Log4j2
public class DefendantServiceImpl implements DefendantService {
    private final  DefendantDao defendantDao;

    @Autowired
    public DefendantServiceImpl(DefendantDao defendantDao) {
        this.defendantDao = defendantDao;
    }
    /**
    *
    * 列表
    */
    @Override
    public PageInfo queryPage(DefendantDTO defendantDTO) {
        log.debug("#queryPage parameter ={}", JSON.toJSONString(defendantDTO));
        PageHelper.startPage(defendantDTO.getPage(), defendantDTO.getRows());
        DefendantExample defendantExample = new DefendantExample();
        DefendantExample.Criteria criteria = defendantExample.createCriteria();
        List<Defendant> defendants = defendantDao.selectByExample(defendantExample);
        return PageInfo.of(defendants);
    }


    /**
     * 信息
     */
    @Override
    public Defendant getById(Integer defendantId){
        log.debug("#getById parameter ={}", defendantId);
        return defendantDao.selectByPrimaryKey(defendantId);
    }

    /**
     * 保存
     */
    @Override

    public Result save(Defendant defendant){
        log.debug("#save parameter ={}", JSON.toJSONString(defendant));
        defendant.setStatusCd(1);
        defendant.setCreatedDt(new Date());
        if(defendantDao.insertSelective(defendant) > 0 ){
            return Result.success("保存成功");
        }else{
            return Result.error("保存失败");
        }
    }

    /**
     * 修改
     */
    @Override

    public Result updateById(Defendant defendant){
         log.debug("#updateById parameter ={}", JSON.toJSONString(defendant));
         DefendantExample defendantExample = new DefendantExample();
         DefendantExample.Criteria criteria = defendantExample.createCriteria();
         criteria.andDefendantIdEqualTo(defendant.getDefendantId());
         if(defendantDao.updateByExampleSelective(defendant,defendantExample) > 0){
             return Result.success("修改成功");
         }else{
             return Result.error("修改失败");
         }
    }
    /**
     * 删除
     */
    @Override

    public Result removeByIds(List<Integer> defendantIds){
        log.debug("#removeByIds parameter ={}", JSON.toJSONString(defendantIds));
        DefendantExample defendantExample = new DefendantExample();
        DefendantExample.Criteria criteria = defendantExample.createCriteria();

            criteria.andDefendantIdIn(defendantIds);

        if(defendantDao.deleteByExample(defendantExample) > 0){
            return Result.success("删除成功");
        }else{
            return Result.error("删除失败");
        }
    }

}